/*
 * @Copyright: 2014~2018 www.yyfax.com Inc. All rights reserved.
 */
package com.wugui.datatx.core.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

/**
 * freemarker
 *
 * @author jiangmy
 * @date 2018年1月15日 下午4:53:23
 * @since 1.0.0
 */
@Slf4j
public class FreemarkerUtils {

	/**
	 * 加载文件
	 *
	 * @param fltFile      flt文件名
	 * @param templatePath flt文件路径 src/template
	 * @param map          数据集合
	 * @return
	 */
	public static <K, V> String geneFileStr(String fltFile, String templatePath, Map<K, V> map) {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
		try (StringWriter out = new StringWriter()) {
			cfg.setClassForTemplateLoading(FreemarkerUtils.class, templatePath);
			cfg.setTemplateLoader(new ClassTemplateLoader(FreemarkerUtils.class.getClassLoader(), templatePath));
			Template template = cfg.getTemplate(fltFile, StandardCharsets.UTF_8.name());

			template.process(map, out);
			out.flush();
			return out.getBuffer().toString();
		} catch (Exception e) {
			try {
				String str = FileUtil.readString(FreemarkerUtils.class.getClassLoader().getResource(fltFile),
						StandardCharsets.UTF_8.name());
				return processTemplateString(str, map);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 组装并输出
	 *
	 * @param template
	 * @param model
	 * @param outputFile
	 * @param encoding *
	 * @author jiangmy
	 * @date 2018年3月1日 上午8:51:44
	 * @since 1.0.0
	 */
	@SuppressWarnings("rawtypes")
	public static void processTemplate(Template template, Map model, File outputFile, String encoding) {
		try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), encoding))) {
			template.process(model, out);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 组装串
	 *
	 * @param templateString
	 * @param model
	 * @return *
	 * @author jiangmy
	 * @date 2018年3月1日 上午8:52:18
	 * @since 1.0.0
	 */
	@SuppressWarnings("rawtypes")
	public static String processTemplateString(String templateString, Map model) {
		return processTemplateString(templateString, model, new Configuration(Configuration.VERSION_2_3_30));
	}

	@SuppressWarnings("rawtypes")
	public static String processTemplateString(String templateString, Map model, Configuration configuration) {
		if (org.apache.commons.lang3.StringUtils.isEmpty(templateString)) {
			return templateString;
		}
		StringWriter out = new StringWriter();
		String s = null;
		try {
			Template template = new Template("templateString...", new StringReader(templateString), configuration);
			template.process(model, out);
			s = out.toString();
			return s;
		} catch (Exception e) {
			throw new IllegalStateException("cannot process templateString:" + templateString + " cause:" + e, e);
		} finally {
			log.debug("freemarker template: {} map: {} result: {}", templateString, model, s);
		}
	}

	private FreemarkerUtils() {
	}

	public static void main(String[] args) {
		System.out
				.println(FreemarkerUtils.geneFileStr("mail.ftl", ".", MapUtil.builder("records", "xxx<br/>").build()));
	}
}
